package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.ApplySalesRefundRequestBean;
import com.ymatou.iapi.trading.operate.parameter.req.RejectRefundBillReq;
import com.ymatou.iapi.trading.operate.service.ApplySalesRefundRequestCall;
import com.ymatou.iapi.trading.operate.service.SalesRefundFailRequestCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.model.YmtRefundBill;
import com.ymttest.database.model.YmtRefundProduct;
import com.ymttest.database.sqlwapper.YmtRefundWapper;

/**
 * 退货/退款失败完成(如拒绝退货)接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SalesRefundFailRequest {
	private static RejectRefundBillReq salesrefundfailrequestBean;
	private static SalesRefundFailRequestCall salesrefundfailrequestCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退货/退款失败完成(如拒绝退货)接口");
	}

	@Before
	public void caseUp() {
		salesrefundfailrequestBean = new RejectRefundBillReq();
		salesrefundfailrequestCall = new SalesRefundFailRequestCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(salesrefundfailrequestCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SalesRefundFailRequest_001() {
		Logger.start(true, "拒绝退货成功");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal amount = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, amount, tradingId);
			// 卖家接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 发货
			orderCallService.dispatched(orderId, sellerId);
			// 申请退款

			ApplySalesRefundRequestBean applysalesrefundrequestBean = orderCallService
					.applySalesRefundRequestInJavaBean(orderId);
			ApplySalesRefundRequestCall applysalesrefundrequestCall = new ApplySalesRefundRequestCall();
			applysalesrefundrequestCall.setData(false,
					applysalesrefundrequestBean);

			applysalesrefundrequestCall.callService();

			String refundBillNo1 = applysalesrefundrequestBean
					.getSalesRefundInfoList().get(0).getRefundBillNo();
			String refundBillNo2 = applysalesrefundrequestBean
					.getSalesRefundInfoList().get(1).getRefundBillNo();

			salesrefundfailrequestBean.setOrderId(orderId);
			salesrefundfailrequestBean.setRefundBillNo(refundBillNo1);
			salesrefundfailrequestCall.setData(salesrefundfailrequestBean);
			salesrefundfailrequestCall.callService();

			// 退第二个商品
			salesrefundfailrequestBean.setRefundBillNo(refundBillNo2);
			salesrefundfailrequestCall.callService();
			Logger.verifyEquals("ok",
					salesrefundfailrequestCall.getReturnData(),
					"验证更新退货退款接口返回正常");

			if (salesrefundfailrequestCall.getReturnData().equals("ok")) {
				// 数据库验证
				YmtRefundWapper ymtRefund = new YmtRefundWapper();
				/*	int isFrozen = new DefaultWapper().getFrozenStatus(orderId);
					Logger.verifyEquals(0, isFrozen, "验证Ymt_Order_Frozen.bFrozen");;*/
				Map m = new HashMap();
				m.put("orderId", orderId);
				m.put("refundBillNo", refundBillNo1);
				YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
				YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m)
						.get(0);
				Logger.comment("校验Ymt_RefundBill表");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(refundBillNo1,
						refundBill.getRefundBillNo(), "验证RefundBillNo");
				Logger.verifyEquals(0, refundBill.getRefundStatus(),
						"验证RefundStatus");
				Logger.verifyEquals(-10, refundBill.getSalesRefundStatus(),
						"验证SettlementStatus");
				Logger.verifyEquals(sellerId, refundBill.getSellerId(),
						"验证SellerId");
				Logger.verifyEquals(userId, refundBill.getBuyerId(),
						"验证BuyerId");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SalesRefundFailRequest_002() {
		Logger.start(false, "订单号不存在");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal amount = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, amount, tradingId);
			// 申请退款
			String refundBillNo = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundBillNo();
			salesrefundfailrequestBean.setOrderId(111111);
			salesrefundfailrequestBean.setRefundBillNo(refundBillNo);
			salesrefundfailrequestCall.setData(salesrefundfailrequestBean);
			salesrefundfailrequestCall.callService();
			Logger.verifyEquals("ok",
					salesrefundfailrequestCall.getReturnData(), "验证更新退货退款接口返回");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SalesRefundFailRequest_003() {
		Logger.start(false, "退款单号不存在");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal amount = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, amount, tradingId);
			salesrefundfailrequestBean.setOrderId(orderId);
			salesrefundfailrequestBean.setRefundBillNo("11111");
			salesrefundfailrequestCall.setData(salesrefundfailrequestBean);
			salesrefundfailrequestCall.callService();
			Logger.verifyEquals("ok",
					salesrefundfailrequestCall.getReturnData(), "验证更新退货退款接口返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SalesRefundFailRequest_004() {
		Logger.start(true, "重复拒绝退货");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal amount = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, amount, tradingId);
			// 申请退款
			String refundBillNo = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundBillNo();

			salesrefundfailrequestBean.setOrderId(orderId);
			salesrefundfailrequestBean.setRefundBillNo(refundBillNo);
			salesrefundfailrequestCall.setData(salesrefundfailrequestBean);
			salesrefundfailrequestCall.callService();
			// 重复拒绝退款
			salesrefundfailrequestCall.callService();
			Logger.verifyEquals("ok",
					salesrefundfailrequestCall.getReturnData(),
					"验证更新退货退款接口返回正常");
			if (salesrefundfailrequestCall.getReturnData().equals("ok")) {
				// 数据库验证
				YmtRefundWapper ymtRefund = new YmtRefundWapper();
				Map m = new HashMap();
				m.put("orderId", orderId);
				m.put("refundBillNo", refundBillNo);
				YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
				YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m)
						.get(0);
				Logger.comment("校验Ymt_RefundBill表");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(refundBillNo, refundBill.getRefundBillNo(),
						"验证RefundBillNo");
				Logger.verifyEquals(0, refundBill.getRefundStatus(),
						"验证RefundStatus");
				Logger.verifyEquals(-10, refundBill.getSalesRefundStatus(),
						"验证SettlementStatus");
				Logger.verifyEquals(sellerId, refundBill.getSellerId(),
						"验证SellerId");
				Logger.verifyEquals(userId, refundBill.getBuyerId(),
						"验证BuyerId");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SalesRefundFailRequest_005() {
		Logger.start(true, "拒绝极速退货成功");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal amount = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, amount, tradingId);
			// 卖家接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 发货
			orderCallService.dispatched(orderId, sellerId);
			// 申请退款

			ApplySalesRefundRequestBean applysalesrefundrequestBean = orderCallService
					.applySalesRefundRequestInJavaBean(orderId);
			ApplySalesRefundRequestCall applysalesrefundrequestCall = new ApplySalesRefundRequestCall();
			applysalesrefundrequestCall.setData(false,
					applysalesrefundrequestBean);

			applysalesrefundrequestCall.callService();

			String refundBillNo1 = applysalesrefundrequestBean
					.getSalesRefundInfoList().get(0).getRefundBillNo();
			String refundBillNo2 = applysalesrefundrequestBean
					.getSalesRefundInfoList().get(1).getRefundBillNo();

			salesrefundfailrequestBean.setOrderId(orderId);
			salesrefundfailrequestBean.setTopspeedRefund(true);
			salesrefundfailrequestBean.setRefundBillNo(refundBillNo1);
			salesrefundfailrequestCall.setData(salesrefundfailrequestBean);
			salesrefundfailrequestCall.callService();

			// 退第二个商品
			salesrefundfailrequestBean.setRefundBillNo(refundBillNo2);
			salesrefundfailrequestCall.callService();
			Logger.verifyEquals("ok",
					salesrefundfailrequestCall.getReturnData(),
					"验证更新退货退款接口返回正常");

			if (salesrefundfailrequestCall.getReturnData().equals("ok")) {
				// 数据库验证
				YmtRefundWapper ymtRefund = new YmtRefundWapper();
				/*	int isFrozen = new DefaultWapper().getFrozenStatus(orderId);
					Logger.verifyEquals(0, isFrozen, "验证Ymt_Order_Frozen.bFrozen");;*/
				Map m = new HashMap();
				m.put("orderId", orderId);
				m.put("refundBillNo", refundBillNo1);
				YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
				YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m)
						.get(0);
				Logger.comment("校验Ymt_RefundBill表");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(refundBillNo1,
						refundBill.getRefundBillNo(), "验证RefundBillNo");
				Logger.verifyEquals(0, refundBill.getRefundStatus(),
						"验证RefundStatus");
				Logger.verifyEquals(-10, refundBill.getSalesRefundStatus(),
						"验证SettlementStatus");
				Logger.verifyEquals(sellerId, refundBill.getSellerId(),
						"验证SellerId");
				Logger.verifyEquals(userId, refundBill.getBuyerId(),
						"验证BuyerId");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(
						salesrefundfailrequestBean.isTopspeedRefund(),
						refundBill.isTopspeedRefund(), "验证OrderId");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
